package com.zhxh.nzpos.setting;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;

import com.zhxh.nzpos.BaseActivity;
import com.zhxh.nzpos.R;
import com.zhxh.nzpos.dao.CardDao;
import com.zhxh.nzpos.dao.OpratorDao;
import com.zhxh.nzpos.model.Member;
import com.zhxh.nzpos.model.Oprator;
import com.zhxh.nzpos.sell.ItemDeleteCallback;
import com.zhxh.nzpos.sell.SettleAccountActivity;

/**
 * 设置中的操作员设定界面
 * 
 * @author wangzt
 *
 */
public class OperatorSettingActivity extends BaseActivity implements ItemDeleteCallback {

	private ArrayList<Member> operatorItems;
	private OperatorListAdapter listAdapter;
	OpratorDao opratorDao;
	CardDao cardDao;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_operator);
        opratorDao = new OpratorDao(this);
        cardDao = new CardDao(this);
        initViews();
    }
	
	private void initViews() {
		ListView listView = (ListView) findViewById(R.id.operator_listview);
		initData();
		// TODO 扫描条码添加一项
	//	final EditText dealOcr = (EditText) findViewById(R.id.operator_id_et);
		ImageView addBtn = (ImageView) findViewById(R.id.operator_add_btn);
		addBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), MemberListActivity.class);
				startActivityForResult(intent, 0);
			}
		});
		
		listAdapter = new OperatorListAdapter(getLayoutInflater(), operatorItems, this);
		listView.setAdapter(listAdapter);
		
		ImageView settleBtn = (ImageView) findViewById(R.id.operator_ok_btn);
		settleBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO计算并传递参数到SettleAccountActivity
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), SettleAccountActivity.class);
				startActivity(intent);
				finish();
			}
		});
/*		ImageView cancelBtn = (ImageView) findViewById(R.id.operator_cancel_btn);
		cancelBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});*/
	}

	
	private void initData() {
		//memberItems = new ArrayList<Member>();
	//	DBHelper dbHelper = new DBHelper(getApplicationContext());
		//dbHelper.openDatabase();	
		operatorItems = new ArrayList();
		ArrayList<Oprator> list = (ArrayList)opratorDao.getAll();
		for(int i = 0 ; i < list.size() ;i++) {
			Oprator oprator = list.get(i);
			Member member = new Member();
			member.setAddress(oprator.getAddress());
			member.setCardNumber(oprator.getMemberId());
			member.setName(oprator.getName());
			member.setPhone(oprator.getPhone());
			member.setShfzID(oprator.getShfzID());
			operatorItems.add(member);
		}
		System.out.println("memberItems====="+operatorItems);
		// Log.i("aaaaaaaaaaaaaa=============", memberItems);
		// Log.i("bbbbbbbbbbbbbbbbbbbbbb=============", memberItems.size());
		
	}
	
	public void onDelete(int pos) {
		operatorItems.remove(pos);
		listAdapter.notifyDataSetChanged();
	}

	@Override
	public void onUpdate() {
		// TODO Auto-generated method stub
		
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (resultCode){  
        case RESULT_OK:  
          Bundle b = data.getExtras();  
          Member member =  (Member)b.getSerializable("member");
         // String string = b.getString("CALCULATION");  
         Oprator  oprator = new Oprator();
         oprator.setMemberId(member.getId());
         oprator.setLimitsId("001");
         oprator.setPassword("654123");
          opratorDao.save(oprator);
          operatorItems.add(0,member);
			if (listAdapter != null) {
				listAdapter.notifyDataSetChanged();
			}   
           
        }  

	}
	
	
}
